package com.joy.ogljoy_01.opengl.ui;

import com.joy.ogljoy_01.opengl.proto.PointF;
import com.joy.ogljoy_01.opengl.proto.RectangleF;
import com.joy.ogljoy_01.opengl.proto.SizeF;

public class Image9 {
	private Image imgBg;
	private PointF position;
	private SizeF size;
	private float width3;
	private float height3;
	
	public static Image9 create() {
		Image9 img9 = new Image9();
		
		img9.init();
		
		return img9;
	}
	
	private void init() {
		imgBg = Image.create("bg.png");
		position = new PointF(0, 0);
		size = SizeF.create(imgBg.width(), imgBg.height());
		width3 = imgBg.width()/3f;
		height3 = imgBg.height()/3f;
	}
	
	public void render() {
		//左上
		imgBg.draw(position.x, position.y, 
				new RectangleF(0, 0, width3, height3));
		//右上
		imgBg.draw(position.x+size.width-width3, position.y, 
				new RectangleF(2*width3, 0, width3, height3));
		//左下
		imgBg.draw(position.x, position.y+size.height-height3, 
				new RectangleF(0, 2*height3, width3, height3));
		//右下
		imgBg.draw(position.x+size.width-width3, position.y+size.height-height3, 
				new RectangleF(2*width3, 2*height3, width3, height3));
		//上中
		float w = size.width - 2*width3;
		int indexW = 1;
		while(w > 0) {
			imgBg.draw(position.x+(indexW++)*width3, position.y, 
					new RectangleF(width3, 0, w >= width3 ? width3 : w, height3));
			
			w -= width3;
		}
		//下中
		w = size.width - 2*width3;
		indexW = 1;
		while(w > 0) {
			imgBg.draw(position.x+(indexW++)*width3, position.y+size.height-height3, 
					new RectangleF(width3, 2*height3, w >= width3 ? width3 : w, height3));
			
			w -= width3;
		}
		//左中
		float h = size.height - 2*height3;
		int indexH = 1;
		while(h >= 0) {
			imgBg.draw(position.x, position.y+(indexH++)*height3, 
					new RectangleF(0, height3, width3, h>=height3 ? height3 : h));
			h -= height3;
		}
		//右中
		h = size.height - 2*height3;
		indexH = 1;
		while(h >= 0) {
			imgBg.draw(position.x+size.width-width3, position.y+(indexH++)*height3, 
					new RectangleF(2*width3, height3, width3, h>=height3 ? height3 : h));
			h -= height3;
		}
		//中间
		w = size.width - 2*width3;
		indexW = 1;
		while(w >= 0) {
			h = size.height - 2*height3;
			indexH = 1;
			while(h >= 0) {
				imgBg.draw(position.x+indexW*width3, position.y+(indexH++)*height3, 
						new RectangleF(width3, height3, w>=width3 ? width3 : w, h>=height3 ? height3 : h));
				h -= height3;
			}
			indexW++;
			w -= width3;
		}
	}

	public PointF getPosition() {
		return position;
	}

	public void setPosition(PointF position) {
		this.position = position;
	}

	public SizeF getSize() {
		return size;
	}

	public void setSize(SizeF size) {
		this.size = size;
	}
}
